
/// 时间转换扩展
extension DurationStringConvertExtention on Duration {

  /// 转换为时间字符串
  String toDurationString() {
    return _duration2String(this);
  }

  /// 转换为时间double
  double toDurationDouble() {
    return _dura2double(this);
  }

  /// 转换为时间double字符串
  String toDurationDoubleString() {
    return _double2DurationString(_dura2double(this));
  }
}

/// double转换为时间字符串扩展
extension DoubleToFormatDuration on double {
  /// double转换为时间字符串
  String get double2FormatTimeString {
    return _double2DurationString(this);
  }
}

/// 时间转换为字符串
String _duration2String(Duration duration) {
  if (duration.inMilliseconds < 0) return "-: negtive";

  String twoDigits(int n) {
    if (n >= 10) return "$n";
    return "0$n";
  }

  String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
  String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));
  int inHours = duration.inHours;
  return inHours > 0 ? "$inHours:$twoDigitMinutes:$twoDigitSeconds" : "$twoDigitMinutes:$twoDigitSeconds";
}

/// 时间转换为double
double _dura2double(Duration d) {
  return d.inMilliseconds.toDouble();
}

/// double转换为时间字符串
String _double2DurationString(double d) {
  return _duration2String(Duration(milliseconds: d.toInt()));
}